package com.wonders.module.auth.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.wonders.framework.dao.impl.BaseDaoImpl;
import com.wonders.framework.entity.vo.Parameter;
import com.wonders.module.auth.dao.RoleAuthDao;
import com.wonders.module.auth.entity.bo.RoleAuth;
import com.wonders.module.auth.entity.vo.RoleAuthVo;

/**
 * 角色权限信息数据层实现
 * 
 * @author WuBin
 * 
 */
@Repository
public class RoleAuthDaoImpl extends BaseDaoImpl implements RoleAuthDao {

	public List<RoleAuth> query(RoleAuthVo roleAuthVo) {
		String hql = "select ra from RoleAuth ra where ra.roleId = :roleId and ra.authType = :authType";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("roleId", roleAuthVo.getRoleId()));
		args.add(new Parameter("authType", roleAuthVo.getAuthType()));
		return super.listByHql(hql, args, RoleAuth.class);
	}

	public void remove(RoleAuthVo roleAuthVo) {
		StringBuffer hql = new StringBuffer(
				"delete from RoleAuth ra where ra.roleId = :roleId");
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("roleId", roleAuthVo.getRoleId()));
		if (roleAuthVo.getAuthType() != null) {
			hql.append(" and ra.authType = :authType");
			args.add(new Parameter("authType", roleAuthVo.getAuthType()));
		}
		super.excuteHql(hql.toString(), args);
	}

	public void removeByRoles(List<Long> roles) {
		String hql = "delete from RoleAuth ra where ra.roleId in (:roles)";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("roles", roles));
		super.excuteHql(hql, args);
	}

	public void removeByAuth(Long authId, Integer authType) {
		String hql = "delete from RoleAuth ra where ra.authId = :authId and ra.authType = :authType";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("authId", authId));
		args.add(new Parameter("authType", authType));
		super.excuteHql(hql, args);
	}

}
